import { MockAdapter } from '@/mock/mock-adapter';
const mock = MockAdapter.getInstance();

import Mock from 'mockjs'
const Random = Mock.Random;
import qs from 'qs';

// 模拟数据
const items: Array<any> = [];
/**
 * 生产数据
 *
 * @param {string} [_srfkey]
 * @returns
 */
function mockData(_srfkey?: string) {
    const srfkey = _srfkey ? _srfkey : Random.guid();
    const srfmajortext = Random.cword(0, 12);
    return {
        
        createman: Random.cword(0, 12), // 建立人


        applyid: srfkey, // 申请标识
        srfkey: srfkey, // 主键
        applyname: srfmajortext, // 申请名称
        srfmajortext: srfmajortext, // 主属性文本
        
        updateman: Random.cword(0, 12), // 更新人


        createdate: Random.datetime(), // 建立时间
        updatedate: Random.datetime(), // 更新时间
        
        wfinstanceid: Random.cword(0, 12), // 工作流实例


        
        wfstate: Random.cword(0, 12), // 工作流状态


        
        wfstep:Mock.mock({"wfstep|1":["ZZ","LD","RS"] }).wfstep, // 工作流步骤


        
        wfversion: Random.cword(0, 12), // 流程版本


        
        applywfstate: Random.cword(0, 12), // 业务状态


    }
}
function mockDatas() {
    for (let i = 0, len = 23; i < len; i++) {
        items.push(mockData());
    }
}
mockDatas();

// 加载数据，以 ? 后作为过滤条件
mock.onGet(new RegExp(/v7\/apply\?[^\/]/)).reply((config: any) => {
    let status = MockAdapter.mockStatus(config);
    if (status !== 200) {
        return [status, null];
    }

    const { url }: { url: string } = config;
    const [, _str] = url.split('?');
    const _params: any = qs.parse(_str);

    let _items: Array<any> = [];
    const { page }: { page: any } = _params;
    const { current, size }: { current: number, size: number } = page;

    if (current && size) {
        _items = [...items.slice((current - 1) * size, current * size)]
    } else {
        _items = [...items];
    }

    const total = items.length;
    return [status, {
        records: _items,
        total: total,
    }];
});

// 指定数据主键 
mock.onGet(new RegExp(/v7\/apply\/[^\/]/)).reply((config: any) => {
    let status = MockAdapter.mockStatus(config);
    if (status !== 200) {
        return [status, null];
    }

    const { url }: { url: string } = config;
    const _arr: Array<string> = url.split('/');
    const _str = _arr[_arr.length - 1];
    if (!_str || Object.is(_str, '')) {
        return [500, null];
    }

    const [srfkey,] = _str.split(';');
    const _item: any = {};

    const _item2 = items.find((item: any) => Object.is(item.srfkey, srfkey));
    if (_item2) {
        Object.assign(_item, _item2, { srfuf: '1' });
    } else if (srfkey && !Object.is(srfkey, '')) {
        Object.assign(_item, mockData(srfkey), { srfuf: '1' });
    }
    return [status, _item];
});

// 加载草稿 
mock.onPost(new RegExp(/v7\/loaddraft\/apply/)).reply((config: any) => {
    let status = MockAdapter.mockStatus(config);
    if (status !== 200) {
        return [status, null];
    }
    const _item: any = {
        createman: null, // 建立人
        applyid: null, // 申请标识
        srfkey: null, // 主键
        applyname: null, // 申请名称
        srfmajortext: null, // 主属性文本
        updateman: null, // 更新人
        createdate: null, // 建立时间
        updatedate: null, // 更新时间
        wfinstanceid: null, // 工作流实例
        wfstate: null, // 工作流状态
        wfstep: null, // 工作流步骤
        wfversion: null, // 流程版本
        applywfstate: null, // 业务状态
        srfuf: '0',
    }

    return [status, _item];
});

// 删除数据，指定数据主键，以 ; 分隔
mock.onDelete(new RegExp(/v7\/apply\/[^\/]/)).reply((config: any) => {
    let status = MockAdapter.mockStatus(config);
    if (status !== 200) {
        return [status, null];
    }

    const { url }: { url: string } = config;
    const _arr: Array<string> = url.split('/');
    const _str = _arr[_arr.length - 1];
    if (!_str || Object.is(_str, '')) {
        return [500, null];
    }

    const params_arr: Array<string> = _str.split(';');
    const state = params_arr.some((key: string) => {
        const index = items.findIndex((item: any) => Object.is(item.srfkey, key));
        if (index == -1) {
            return true;
        }
        items.splice(index, 1);
        return false;
    });
    if (state) {
        return [500, null];
    }

    return [status, {}];
});


// 保存(save)和更新(update)数据，指定数据主键
mock.onPut(new RegExp(/v7\/apply\/[^\/]/)).reply((config: any) => {
    let status = MockAdapter.mockStatus(config);
    if (status !== 200) {
        return [status, null];
    }

    const { data }: { data: string } = config;
    let _item: any | null = {};
    try {
        Object.assign(_item, JSON.parse(data));
        _item.srfmajortext = _item.applyname;
    } catch (error) {
        status = 500;
        _item = null;
    }

    const index = items.findIndex((item: any) => Object.is(item.srfkey, _item.srfkey));
    if (index !== -1) {
        Object.assign(items[index], _item);
    } else {
        items.push(_item);
    }

    return [status, _item];
});


// 创建 （create） 数据
mock.onPost(new RegExp(/v7\/apply/)).reply((config: any) => {
    let status = MockAdapter.mockStatus(config);
    if (status !== 200) {
        return [status, null];
    }
    const { data }: { data: string } = config;
    let _item: any | null = {};
    try {
        Object.assign(_item, JSON.parse(data));
        _item.srfmajortext = _item.applyname;
        _item.applyid = Random.guid();
        _item.srfkey = _item.applyid;
        Object.assign(_item, { srfuf: '1' });
        items.push(_item);
    } catch (error) {
        status = 500;
        _item = null;
    }
    return [status, _item];
});



//工作流相关
/**
 * 工作流启动
 *
 * @param {string} srfkey
 * @param {*} version
 */
function start(srfkey: string, version: any) {
    let wfversion: any = getWFVersion(version);
    const _item2 = items.find((item: any) => Object.is(item.srfkey, srfkey));
    let node = wfversion.nodes.find((item: any) => Object.is(item.from, wfversion.startnode));
    _item2.wfstep = node.wfstepvalue;
    _item2.applywfstate = 1;
}

/**
 * 改变工作流步骤
 *
 * @param {string} srfkey
 * @param {*} version
 * @param {string} action
 */
function changeStep(srfkey: string, version: any, action: string) {
    let wfversion: any = getWFVersion(version);
    const _item2 = items.find((item: any) => Object.is(item.srfkey, srfkey));
    let link = wfversion.links.find((item: any) => Object.is(item.name, action));
    let node = wfversion.nodes.find((item: any) => Object.is(item.id, link.to));
    _item2.wfstep = node.wfstepvalue;
}

/**
 * 工作流完成
 *
 * @param {string} srfkey
 */
function finish(srfkey: string) {
    const _item2 = items.find((item: any) => Object.is(item.srfkey, srfkey));
    _item2.wfstep = "";
    _item2.applywfstate = 2;
}

/**
 * 获取工作流版本
 *
 * @param {*} version
 * @returns
 */
function getWFVersion(version: any) {
    if (version == 1)
        return '';
}

mock.onPost(new RegExp(/wf\/apply\/v1\/zz/)).reply((config: any) => {
    let status = MockAdapter.mockStatus(config);
    if (status !== 200) {
        return [status, null];
    }
    let _items: Array<any> = [];
    for (var index in items) {
        if(items[index].wfstep=='ZZ'){
            _items.push(items[index]);
        }
        
    }
    const total = _items.length;
    return [status, {
        records: _items,
        total: total,
    }];
});

mock.onPost(new RegExp(/wf\/apply\/v1\/c2\/[^\/]/)).reply((config: any) => {
    let status = MockAdapter.mockStatus(config);
    if (status !== 200) {
        return [status, null];
    }
    const { url }: { url: string } = config;
    const _arr: Array<string> = url.split('/');
    const _str = _arr[_arr.length - 1];
    if (!_str || Object.is(_str, '')) {
        return [500, null];
    }
    const [srfkey,] = _str.split(';');
    let data = JSON.parse(config.data) 
    const _item2 = items.find((item: any) => Object.is(item.srfkey, srfkey));
    _item2.wfstep='LD';
    return [status, {}];
});

mock.onPost(new RegExp(/wf\/apply\/v1\/c22\/[^\/]/)).reply((config: any) => {
    let status = MockAdapter.mockStatus(config);
    if (status !== 200) {
        return [status, null];
    }
    const { url }: { url: string } = config;
    const _arr: Array<string> = url.split('/');
    const _str = _arr[_arr.length - 1];
    if (!_str || Object.is(_str, '')) {
        return [500, null];
    }
    const [srfkey,] = _str.split(';');
    let data = JSON.parse(config.data) 
    const _item2 = items.find((item: any) => Object.is(item.srfkey, srfkey));
    _item2.wfstep='';
    _item2.applywfstate='';
    return [status, {}];
});

mock.onPost(new RegExp(/wf\/apply\/v1\/saveandstart\/[^\/]/)).reply((config: any) => {
    let status = MockAdapter.mockStatus(config);
    if (status !== 200) {
        return [status, null];
    }
    const { url }: { url: string } = config;
    const _arr: Array<string> = url.split('/');
    const _str = _arr[_arr.length - 1];
    if (!_str || Object.is(_str, '')) {
        return [500, null];
    }
    const [srfkey,] = _str.split(';');
    let data = JSON.parse(config.data)
    const _item2 = items.find((item: any) => Object.is(item.srfkey, srfkey));
    _item2.wfstep='ZZ';
    _item2.applywfstate='1';
    return [status, {}];
});    
mock.onPost(new RegExp(/wf\/apply\/v1\/ld/)).reply((config: any) => {
    let status = MockAdapter.mockStatus(config);
    if (status !== 200) {
        return [status, null];
    }
    let _items: Array<any> = [];
    for (var index in items) {
        if(items[index].wfstep=='LD'){
            _items.push(items[index]);
        }
        
    }
    const total = _items.length;
    return [status, {
        records: _items,
        total: total,
    }];
});

mock.onPost(new RegExp(/wf\/apply\/v1\/c3\/[^\/]/)).reply((config: any) => {
    let status = MockAdapter.mockStatus(config);
    if (status !== 200) {
        return [status, null];
    }
    const { url }: { url: string } = config;
    const _arr: Array<string> = url.split('/');
    const _str = _arr[_arr.length - 1];
    if (!_str || Object.is(_str, '')) {
        return [500, null];
    }
    const [srfkey,] = _str.split(';');
    let data = JSON.parse(config.data) 
    const _item2 = items.find((item: any) => Object.is(item.srfkey, srfkey));
    _item2.wfstep='RS';
    return [status, {}];
});

mock.onPost(new RegExp(/wf\/apply\/v1\/c33\/[^\/]/)).reply((config: any) => {
    let status = MockAdapter.mockStatus(config);
    if (status !== 200) {
        return [status, null];
    }
    const { url }: { url: string } = config;
    const _arr: Array<string> = url.split('/');
    const _str = _arr[_arr.length - 1];
    if (!_str || Object.is(_str, '')) {
        return [500, null];
    }
    const [srfkey,] = _str.split(';');
    let data = JSON.parse(config.data) 
    const _item2 = items.find((item: any) => Object.is(item.srfkey, srfkey));
    _item2.wfstep='';
    _item2.applywfstate='';
    return [status, {}];
});

mock.onPost(new RegExp(/wf\/apply\/v1\/rs/)).reply((config: any) => {
    let status = MockAdapter.mockStatus(config);
    if (status !== 200) {
        return [status, null];
    }
    let _items: Array<any> = [];
    for (var index in items) {
        if(items[index].wfstep=='RS'){
            _items.push(items[index]);
        }
        
    }
    const total = _items.length;
    return [status, {
        records: _items,
        total: total,
    }];
});

mock.onPost(new RegExp(/wf\/apply\/v1\/c4\/[^\/]/)).reply((config: any) => {
    let status = MockAdapter.mockStatus(config);
    if (status !== 200) {
        return [status, null];
    }
    const { url }: { url: string } = config;
    const _arr: Array<string> = url.split('/');
    const _str = _arr[_arr.length - 1];
    if (!_str || Object.is(_str, '')) {
        return [500, null];
    }
    const [srfkey,] = _str.split(';');
    let data = JSON.parse(config.data) 
    const _item2 = items.find((item: any) => Object.is(item.srfkey, srfkey));
    _item2.wfstep='';
    _item2.applywfstate='';
    return [status, {}];
});

